import { HttpException, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Web_classify } from './entities/web-classify';
import { Repository } from 'typeorm';
import { Web_link } from './entities/web-link.entity';


@Injectable()
export class WebLinkService {

  @InjectRepository(Web_classify)
  private webclassify:Repository<Web_classify>

  @InjectRepository(Web_link)
  private webLink:Repository<Web_link>


  async createClassify(createWebLinkDto: any) {
    return await this.webclassify.save(createWebLinkDto)
  }
  async updateClassify(obj: any) {
    if(!obj.id){
      return '修改失败,请传入修改的id'
    }
    if(typeof obj.id == "string"){
      obj.id = +obj.id
    }
    return await this.webclassify.save(obj)
  }
  async getClassifyList() {
    return await this.webclassify.find()
  }

  async deleteClassify(id:number) {
    const data = await this.webclassify.delete(id)
    if(data.affected>0){
      return '删除成功'
    }
    return '删除失败'
  }


  //新增链接
  async createLink(obj: any) {
    const webclassify = await this.webclassify.findOneBy({
      id:obj.classify_id
    })
    console.log('分类的对象',webclassify)
    if(!webclassify){
      return '分类id错误'
    }
    obj.category = webclassify
    return await this.webLink.save(obj)
  }
  async updateLink(obj: any) {
    if(!obj.id){
      return '修改失败,请传入修改的id'
    }
    if(typeof obj.id == "string"){
      obj.id = +obj.id
    }
    return await this.webLink.save(obj)
  }
  async getLinkList() {
    return await this.webLink.find()
  }

  async deleteLink(id:number) {
    const data = await this.webLink.delete(id)
    if(data.affected>0){
      return '删除成功'
    }
    return '删除失败'
  }
 
}
